﻿using LD.Host.Tool;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class VmRunOneFrm : FrmToolBase
    {
        public VmRunOneFrm()
        {
            InitializeComponent();

            this.Text = TB_Tool.Name;
            this.btn_ok.Click += Btn_ok_Click;
            this.Shown += ToolReadImageFrm_Shown;
        }

        private void ToolReadImageFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var param = new VmRunParam();
            param.FlowName = this.cob_camera.Text;
            //回写PLC参数
            if (param.EchoPLC == null)
                param.EchoPLC = new List<EchoPLC>();
            if (!string.IsNullOrEmpty(this.txt_plcAddress.Text))
                param.EchoPLC.Add(new EchoPLC()
                {
                    Address = txt_plcAddress.Text,
                    DataType = "Int16",
                    IsEcho = true
                }); ;

            var paramString = Newtonsoft.Json.JsonConvert.SerializeObject(param);
            TB_Tool.Param = paramString;
        }

        private void Init()
        {
            if (TB_Tool.Param == null)
                return;
            var param = Newtonsoft.Json.JsonConvert.DeserializeObject<VmRunParam>(TB_Tool.Param);
            if (param != null)
            {
                this.cob_camera.Text = param.FlowName;

                //回写PLC信息展示
                if (param.EchoPLC != null && param.EchoPLC.Count > 0)
                    this.txt_plcAddress.Text = param.EchoPLC[0].Address;
            }
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
